import { Button, Tag, Tooltip } from 'antd';
import { Fragment } from 'react';
import Cls from 'classnames';
import { SchemaManageAPI } from '@/components/SchemaComponent/SchemaManage/type';
import { PlusOutlined, RollbackOutlined, EditOutlined, CloseCircleOutlined } from '@ant-design/icons';
import { ColumnsRenderFun, DictionaryRenderFun, SHTH_CHECKSTATE } from '../../Acommon';
import { getPosIDs, getTenantID, getUserID, removeDateHmsStr, tooltipLength } from '@/utils';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { ManageCommonAPI } from '@/pages/type';
import pageStyles from '@/common/pageLayout.less';
import { FSBG_STATE_QC, FSBG_STATE_SHZ } from '../common/Format';
import { ILoadMapTypeAll } from '@/common';

/**
 * @getSorter
 * @description 排序
 * @param prev
 * @param next
 * @returns
 */
const getSorter = (prev: Record<string, any>, next: Record<string, any>): number => (prev > next ? 1 : -1);

/** 操作按钮的显示 */
const Operation = (curRes: Record<string, any>, operateFuns: Record<string, any>, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible:
        curRes?.['/purchaseSourceChangeController/edit'] &&
        ((record?.state == FSBG_STATE_QC() && !record?.checkState) || (record?.state == FSBG_STATE_SHZ() && record?.checkState == SHTH_CHECKSTATE())) &&
        record?.applyerID == getUserID() &&
        record?.applyPositionID == getPosIDs(),
    },
    {
      icon: <CloseCircleOutlined />,
      btnName: '作废',
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible:
        curRes?.['/purchaseSourceChangeController/cancel'] &&
        record?.state == FSBG_STATE_QC() &&
        !record?.checkState &&
        record?.createPositionID == getPosIDs() &&
        record?.createrID == getUserID(),
    },
    // {
    //   icon: <RollbackOutlined />,
    //   btnName: '撤销',
    //   clickFnc: operateFuns,
    //   clickType: 'onInvalid',
    //   isVisible:
    //     curRes['/purchaseSourceChangeController/invalid'] &&
    //     record?.id &&
    //     record?.applyerID == getUserID() &&
    //     record?.applyPositionID == getPosIDs() &&
    //     (record?.state == FSBG_STATE_SHZ() || (record?.state == FSBG_STATE_QC() && record?.checkState == SHTH_CHECKSTATE())),
    // },
  ];

  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

/**
 * @getProcurementMethodManageTableColumns
 * @description 变更采购方式管理数据表格信息展示和操作
 */
const getProcurementMethodManageTableColumns = ({ operation }: any, curRes): Record<string, any>[] => {
  const { onCheck, onView, onCancel } = operation;
  return [
    // {
    //   title: '编号',
    //   dataIndex: 'id',
    //   key: 'id',
    //   visible: true,
    //   width: 170,
    //   fixed: 'left',
    //   render: (text: string) => ColumnsRenderFun({ text }),
    // },
    {
      title: '承办部门/团队',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.createDepartmentName, b?.createDepartmentName),
      width: 250,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '采购项目名称',
      dataIndex: 'purchProjectName',
      key: 'purchProjectName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.purchProjectName, b?.purchProjectName),
      width: 200,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '标包名称',
      dataIndex: 'packName',
      key: 'packName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.packName, b?.packName),
      width: 200,
      render: (text: string, record: Record<string, any>) => {
        return (
          <a
            onClick={() => {
              if (curRes?.['/purchaseSourceChangeController/getSourceChangeById/{id}/{currentUserID}']) {
                onView(record);
              }
            }}
            style={{ width: '100%' }}
          >
            <Tooltip title={tooltipLength(`${text}(${record?.packCode})`, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{`${text}(${record?.packCode})` || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '变更前采购方式',
      dataIndex: 'procureDraftName',
      key: 'procureDraftName',
      width: 180,
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.procureDraft, b?.procureDraft),
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '拟采用采购方式',
      dataIndex: 'procureRealName',
      key: 'procureRealName',
      width: 180,
      sorter: (a: any, b: any) => getSorter(a?.procureReal, b?.procureReal),
      visible: true,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '预算金额(元)',
      dataIndex: 'expectSum',
      key: 'expectSum',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.expectSum, b?.expectSum),
      width: 200,
      render: (text: string, record) => {
        return Number(text).toFixed(2);
      },
    },
    {
      title: '状态',
      dataIndex: 'stateName',
      key: 'stateName',
      sorter: (a: any, b: any) => getSorter(a?.stateName, b?.stateName),
      visible: true,
      width: 140,
      render: (text: string) => DictionaryRenderFun({ text, isState: true }),
    },
    {
      title: '执行状态',
      dataIndex: 'checkStateName',
      key: 'checkStateName',
      sorter: (a: any, b: any) => getSorter(a?.checkState, b?.checkState),
      visible: true,
      width: 140,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '审批方式',
      dataIndex: 'applyFlag',
      key: 'applyFlag',
      sorter: (a: any, b: any) => getSorter(a?.applyFlag, b?.applyFlag),
      visible: true,
      width: 160,
      render: (text: boolean) => {
        return text ? <Tag color="blue">线下</Tag> : <Tag color="orange">线上</Tag>;
      },
    },
    {
      title: '经办人',
      dataIndex: 'applyerName',
      key: 'applyerName',
      sorter: (a: any, b: any) => getSorter(a?.applyerName, b?.applyerName),
      width: 120,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    // {
    //   title: '项目费用（元）',
    //   dataIndex: 'projectPrice',
    //   key: 'projectPrice',
    //   sorter: (a: any, b: any) => getSorter(a?.projectPrice, b?.projectPrice),
    //   visible: true,
    //   width: 200,
    //   render: (text: string) => {
    //     return <span>{text ? Number(text).toFixed(2) : '--'}</span>;
    //   },
    // },
    // {
    //   title: '申请类型',
    //   dataIndex: 'applyTypName',
    //   key: 'applyTypName',
    //   sorter: (a: any, b: any) => getSorter(a?.applyTypName, b?.applyTypName),
    //   visible: true,
    //   width: 120,
    //   render: (text: string) => DictionaryRenderFun({ text }),
    // },

    {
      title: '联系电话',
      dataIndex: 'applyPhone',
      key: 'applyPhone',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.applyPhone, b?.applyPhone),
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: (a: any, b: any) => getSorter(a?.createTime, b?.createTime),
      width: 190,
      visible: true,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '创建人',
      dataIndex: 'createrName',
      key: 'createrName',
      sorter: (a: any, b: any) => getSorter(a?.createrName, b?.createrName),
      width: 120,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      sorter: (a: any, b: any) => getSorter(a?.createPositionName, b?.createPositionName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      sorter: (a: any, b: any) => getSorter(a?.createDepartmentName, b?.createDepartmentName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人公司',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      sorter: (a: any, b: any) => getSorter(a?.createCompanyName, b?.createCompanyName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      sorter: (a: any, b: any) => getSorter(a?.modifyTime, b?.modifyTime),
      width: 190,
      visible: false,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      visible: false,
      width: 120,
      sorter: (a: any, b: any) => getSorter(a?.modifyerName, b?.modifyerName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.modifyPositionName, b?.modifyPositionName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.modifyDepartmentName, b?.modifyDepartmentName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人公司',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.modifyCompanyName, b?.modifyCompanyName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '经办时间',
      dataIndex: 'applyTime',
      key: 'applyTime',
      sorter: (a: any, b: any) => getSorter(a?.applyTime, b?.applyTime),
      width: 190,
      visible: false,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },

    {
      title: '经办人岗位',
      dataIndex: 'applyPositionName',
      key: 'applyPositionName',
      sorter: (a: any, b: any) => getSorter(a?.applyPositionName, b?.applyPositionName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '经办人部门',
      dataIndex: 'applyDepartmentName',
      key: 'applyDepartmentName',
      sorter: (a: any, b: any) => getSorter(a?.applyDepartmentName, b?.applyDepartmentName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '经办人公司',
      dataIndex: 'applyCompanyName',
      key: 'applyCompanyName',
      sorter: (a: any, b: any) => getSorter(a?.applyCompanyName, b?.applyCompanyName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人',
      dataIndex: 'cancelerName',
      key: 'cancelerName',
      visible: false,
      width: 120,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'cancelTime',
      visible: false,
      width: 180,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '作废类型',
      dataIndex: 'cancelTypeName',
      key: 'cancelTypeName',
      visible: false,
      width: 180,
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      sorter: (a: any, b: any) => getSorter(a?.cancelReason, b?.cancelReason),
      visible: false,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      sorter: (a: any, b: any) => getSorter(a?.cancelReason, b?.cancelReason),
      visible: false,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 170,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: any, record: Record<string, any>) => {
        return Operation(curRes, operation, record);
      },
    },
    // {
    //   title: '操作',
    //   dataIndex: 'operation',
    //   key: 'operation',
    //   width: 230,
    //   visible: true,
    //   unDrag: true,
    //   align: 'left',
    //   fixed: 'right',
    //   // render: (_: string, record: Record<string, any>) => {
    //   //   return (
    //   //     <Fragment>
    //   //       <Button
    //   //         type={'link'}
    //   //         size={'small'}
    //   //         className="manageBtntTxtStyle"
    //   //         icon={<DeliveredProcedureOutlined />}
    //   //         onClick={() => {
    //   //           onCheck(record);
    //   //         }}
    //   //       >
    //   //         审核
    //   //       </Button>
    //   //       <Button
    //   //         type={'link'}
    //   //         size={'small'}
    //   //         className="manageBtntTxtStyle"
    //   //         icon={<EyeOutlined />}
    //   //         onClick={() => {
    //   //           onView(record);
    //   //         }}
    //   //       >
    //   //         查看
    //   //       </Button>
    //   //       <Button
    //   //         type={'link'}
    //   //         size={'small'}
    //   //         danger
    //   //         className={Cls('manageBtntTxtStyle', 'manageColorRedBtn')}
    //   //         icon={<CloseCircleOutlined />}
    //   //         onClick={() => {
    //   //           onCancel(record);
    //   //         }}
    //   //       >
    //   //         作废
    //   //       </Button>
    //   //     </Fragment>
    //   //   );
    //   // },
    //   render: (text: string | null, record: Record<string, any>) => {
    //     return <div style={{ display: 'flex' }}>{Operation(curRes, operation, record)}</div>;
    //   },
    // },
  ];
};
/**
 * @getProcurementMethodManageTableHeaderActions
 * @description 变更采购方式管理表格顶部自定义按钮
 * @returns SchemaManageAPI.IActionBtnList[]
 */
const getProcurementMethodManageTableHeaderActions = (operation: any, curRes): SchemaManageAPI.IActionBtnList[] => {
  const { onAdd } = operation;
  return [
    {
      title: '添加',
      visible: curRes?.['/purchaseSourceChangeController/add'],
      type: 'primary',
      icon: <PlusOutlined />,
      onClick: () => {
        onAdd();
      },
    },
  ];
};

export { getProcurementMethodManageTableColumns, getProcurementMethodManageTableHeaderActions };
